package generator.statistics;

import generator.equation.Arith;
import generator.structure.Pair;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;

public final class NumericalSet implements Serializable {

	private int Partition;
	Arith compute = new Arith();
	private int decimalPoint = 3;
	int currentColumn;
	
	private HashMap<Integer, Pair<Integer, Integer>> category = new HashMap<Integer, Pair<Integer, Integer>>();
	
	private HashMap<Integer, Pair<Double,Double>> columnInitial = new HashMap<Integer, Pair<Double,Double>>();
	
	private Map<Integer,HashMap<String,Integer>> columnDistri = new HashMap<Integer,HashMap<String,Integer>>();
	
	private Set<Integer> columnSet;
	
	
	public NumericalSet(int partition){
		
		Partition = partition; 
		
	}
	
	public void clear(){
		
		columnInitial.clear();
		columnDistri.clear();
		category.clear();
		
	}
	
	public void setInitial(HashMap<Integer, Pair<Integer, Integer>> Category){
//		System.out.println("-----------------setInitial-----------------");
		category = Category;
		
		columnSet = new HashSet<Integer>(Category.keySet());
		for(Iterator<Integer> ite = Category.keySet().iterator(); ite.hasNext();){
			
			int column = ite.next();
//			System.out.println(column);
			HashMap<String, Integer> Distribution = new HashMap<String, Integer>();
			columnDistri.put(column, Distribution);
			Pair<Double,Double> offset_max = new Pair<Double,Double>(0.0, 0.0);
			columnInitial.put(column,offset_max);
			
		}
		
	}
	
	public void recordMax(int column, double frequence){
		
		double max = columnInitial.get(column).second_;
		
//		System.out.println("max: " + max + " value " + frequence);
		
		if(Double.compare(frequence, max) == 1){
			columnInitial.get(column).second_ = frequence;
	//		System.out.println("hi");
		}
		
	}
	
	
	public void setOffset(int column){
		
//		System.out.println("----------------------setOffset-------------------");
		double offset = compute.div(columnInitial.get(column).second_, Partition, decimalPoint);
		columnInitial.get(column).first_ = offset;
//		System.out.println("column: " + column + " max: " + columnInitial.get(column).second_ + " offset: " + offset);
	}
	
	////////////////////////////////////////////////////////////
	
	public void setColumn(int column){
		currentColumn = column;
	}
	
	
	public String getANDaddValue(Double value){
		
		double offset = columnInitial.get(currentColumn).first_;
		HashMap<String, Integer> Distribution = columnDistri.get(currentColumn);
		
		String key = value / offset + "";
		
		if(Distribution.containsKey(key)){
			int count = Distribution.get(key) + 1;
			Distribution.put(key, count);
		} else {
			Distribution.put(key, 1);
		}	
		
		return key;
			
	}
	
	public HashMap<String,Integer> getDistribution(int column){
		return columnDistri.get(column);
	}
	
	////////////////////////////////////////////////////////////
	
	public Integer[] getcolumnSet(){
		Integer[] columns = new Integer[columnSet.size()];
		columnSet.toArray(columns);
		return columns;
	}
	
	
	public String getKey(int column, String Value){
		
		double value = Double.parseDouble(Value);
		double offset = columnInitial.get(column).first_;
		double result;
		result = ((int) (value / offset) + 1) * offset;
		String key = "" + result;
//		System.out.println("column-" + column + " value :" + value + " key: " + key + " offset: " + offset + " max: " + columnInitial.get(column).second_);
		return key;
			
	}
	
	public int getCategory(int column){
		int cate = category.get(column).first_;
		return cate;
	}
	
	Random r = new Random(System.currentTimeMillis());
	public String genNumerical(int column, String KeyString){
		
//		System.out.println("genNumerical:");
		double offset = columnInitial.get(column).first_;
		
		double random = Math.abs(r.nextDouble() * offset + (Double.parseDouble(KeyString) - offset));
//		System.out.println(column + " " + random + " " + offset);
		String result = random + "";
		return result;
		
	}

	
}
